﻿using System;
using System.Collections.Generic;
using SmartHRMS.Presentation.Account;
using SmartHRMS.Model.Account;

namespace SmartHRMS.UI.Web.Modules.Administration.Account
{
    public partial class UserList1 : System.Web.UI.UserControl, IUserListView
    {
        private UserListPresenter _Presenter;

        private IList<User> localUserList = null;

        private IList<User> localFilteredUserList = null;

        protected void Page_Init(object sender, EventArgs e)
        {
            _Presenter = new UserListPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _Presenter.Display();

                string[] alphabet = "A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(';');
                rptAlphabet.DataSource = alphabet;
                rptAlphabet.DataBind();
            }
        }

        #region IBaseView Members

        public void ErrorMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlError.Visible = false;
            }
            else
            {
                pnlError.Visible = true;
            }
            this.litErrorMessage.Text = Message + "<br/>";
        }

        public void InfoMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlInfo.Visible = false;
            }
            else
            {
                pnlInfo.Visible = true;
            }
            this.litInfoMessage.Text = Message + "<br/>";
        }

        public void WarringMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                //    pnlWarr.Visible = false;
            }
            else
            {
                //   pnlWarr.Visible = true;
            }
            //  this.litWarrMessage.Text = Message + "<br/>";
        }

        public void ViewTitle(string title)
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region IUserListView Members

        IList<User> IUserListView.User
        {
            set
            {
                localUserList = value;
                this.grdUsers.DataSource = localUserList;
                this.grdUsers.DataBind();
            }
        }

        public IList<User> FilteredUsers
        {
            set 
            { 
                localFilteredUserList = value;
            }
        }

        #endregion

        private void BindUsers(bool reloadAllUsers)
        {
            if (reloadAllUsers)
            {
                _Presenter.Display();
            }
            IList<User> users = null;

            string searchText = "";
            if (!string.IsNullOrEmpty(grdUsers.Attributes["SearchText"]))
                searchText = grdUsers.Attributes["SearchText"];

            bool searchByEmployeeCode = false;
            if (!string.IsNullOrEmpty(grdUsers.Attributes["SearchByEmployeeCode"]))
                searchByEmployeeCode = bool.Parse(grdUsers.Attributes["SearchByEmployeeCode"]);

            if (searchText.Length > 0)
            {
                if (searchByEmployeeCode)
                {
                    _Presenter.Filter(1, searchText);
                    users = localFilteredUserList;
                }
                else
                {
                    _Presenter.Filter(0, searchText);
                    users = localFilteredUserList;
                }
            }
            else
            {
                _Presenter.Display();
                users = localUserList;
            }

            grdUsers.DataSource = users;
            grdUsers.DataBind();
        }

        protected void btnNewUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserDetailPage.aspx?id=0");
        }

        protected void grdUsers_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            string deleteComand = e.CommandName.ToLower().ToString();
            if (deleteComand == "deleteuser")
            {
                _Presenter.RemoveUserBy(new Guid(e.CommandArgument.ToString()));
            }
        }

        protected void rptAlphabet_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            grdUsers.Attributes.Add("SearchByEmployeeCode", false.ToString());
            if (e.CommandArgument.ToString().Length == 1)
            {
                grdUsers.Attributes.Add("SearchText", e.CommandArgument.ToString() + "%");
                BindUsers(false);
            }
            else
            {
                grdUsers.Attributes.Add("SearchText", "");
                BindUsers(false);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bool searchByEmployeeCode = (ddlSearchTypes.SelectedItem.Value.ToString() == "EmployeeCode");
            grdUsers.Attributes.Add("SearchText",  "%"+ txtSearchText.Text+"%" );
            grdUsers.Attributes.Add("SearchByEmployeeCode", searchByEmployeeCode.ToString());
            BindUsers(false);
        }
    }
}